var express = require('express');
var router = express.Router();

var mysql = require('mysql');
var connection = mysql.createConnection({
    // host: '192.168.1.194',
    // user: 'skl',
    // password: 'skl123',
    // database: 'orderdata'
    host: 'localhost',
    port: 3306, // MySQL 默认端口为 3306
    user: 'root',  //用户名
    password: 'woaini.1.2', //用户密码
    database: 'orderdata',  //数据库
    useConnectionPooling: true  
});

connection.connect();
// 获取数据
router.get('/data', function (req,res,next) {
    connection.query('SELECT * from miao', function (error, results, fields) {
        if (error) throw error;
        res.send(results)
    });
})
// ////// 秒杀
// 添加数据
router.post('/adddata',function(req,res,next){
    connection.query("INSERT INTO miao set ?",req.body.obj,function(error,results,fields){
        if(error) throw error;
        res.send(results)
      })
})
// 删除数据
router.post('/deldata',function(req,res,next) {
    connection.query(`DELETE FROM miao WHERE ID = ${req.body.id}`, function (error, results, fields) {
        if (error) throw error;
        console.log(results,'data');
        res.send(results)
    });
})
// 改变选中框
router.post('/changechecked',function (req,res,next) {
    console.log(req.body.id);
})
// 编辑数据
router.post('/editdata',function (req,res,next) {
    console.log(req.body.obj);
    connection.query(`UPDATE miao SET type = ${req.body.obj.type} , title = "${req.body.obj.title}" where id = "${req.body.obj.id}"`, function (error, results, fields) {
        if (error) throw error;
        res.send(results)
    });
})
/////// 优惠卷
// 删除
router.post('/deldatajuan',function (req,res,next) {
    connection.query(`DELETE FROM miao WHERE ID = ${req.body.id}`, function (error, results, fields) {
        if (error) throw error;
        res.send(results)
    });
})
// 编辑
router.post('/editjuan',function(req,res,next){
    console.log(req.res.obj.id);
    connection.query(`UPDATE miao SET expiryData = ${req.body.obj.expiryData} ,faceValue="${req.body.obj.faceValue}" pouponType = "${req.body.obj.pouponType}" where id = "${req.body.obj.id}"`,function(error , results,fields){
        if(error) throw error;
        res.send(results)
    })
})


module.exports = router;